package com.guodu.dao.impl;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.FilemanagerDao;
import com.guodu.entity.Filemanager;
import com.guodu.entity.Units;
import com.guodu.vo.FilemanagerQueryVo;


@Repository("filemanagerDao")
public class FilemanagerDaoImpl extends BaseDao implements FilemanagerDao {

	@Override
	public long findSizeByVo(FilemanagerQueryVo fqv, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from Filemanager m where 1=1");
		if (fqv!=null) {
			if(fqv.getCreateBeginTime()!=null&&fqv.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+fqv.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(fqv.getCreateEndTime()!=null&&fqv.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+fqv.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (fqv.getUnitsId()==null) {
				if (fqv.getFunitsId()!=null&&!"".equals(fqv.getFunitsId())) {
					hql.append(" and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units f where u.unitsId=f.unitsId and f.unitsId = "+fqv.getFunitsId()+" ))");
				}
			}
			if (fqv.getUnitsId()!=null&&!"".equals(fqv.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units f where u.unitsId=f.unitsId and f.unitsId = "+fqv.getUnitsId()+" ))");
			}
		}
		return super.findInt(hql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Filemanager> findByVo(FilemanagerQueryVo fqv, Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from Filemanager m where 1=1");
		if (fqv!=null) {
			if(fqv.getCreateBeginTime()!=null&&fqv.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+fqv.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(fqv.getCreateEndTime()!=null&&fqv.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+fqv.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (fqv.getUnitsId()==null) {
				if (fqv.getFunitsId()!=null&&!"".equals(fqv.getFunitsId())) {
					hql.append(" and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units f where u.unitsId=f.unitsId and f.unitsId = "+fqv.getFunitsId()+" ))");
				}
			}
			if (fqv.getUnitsId()!=null&&!"".equals(fqv.getUnitsId())) {
				hql.append(" and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units f where u.unitsId=f.unitsId and f.unitsId = "+fqv.getUnitsId()+" ))");
			}
		}
		hql.append(" order by filemanagerId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public void saveFile(Filemanager filemanager) {
		// TODO Auto-generated method stub
		super.save(filemanager);
	}

	@Override
	public void delZi(String string) {
		// TODO Auto-generated method stub
		Filemanager filemanager = (Filemanager) super.findById(Filemanager.class, string);
			String path = ServletActionContext.getServletContext().getRealPath("/")+ "/upload/Filemanager/";
			File file = new File(path+filemanager.getFileRealname());
			if (file.isFile() && file.exists()) {  
				file.delete();
			}
	}
	
	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(Filemanager.class, delIds);
	}

	@Override
	public Filemanager findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (Filemanager) super.findById(Filemanager.class, purchaseId);
	}

}
